package com.zy.ems.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zy.ems.common.base.BaseDao;
import com.zy.ems.common.base.BaseService;
import com.zy.ems.common.exception.ApiException;
import com.zy.ems.common.page.PaginationResult;
import com.zy.ems.common.util.DateUtil;
import com.zy.ems.common.util.Md5Util;
import com.zy.ems.common.util.StringUtils;
import com.zy.ems.dao.UserDao;
import com.zy.ems.model.UserEntity;
import com.zy.ems.utils.enums.Constant;
import com.zy.ems.utils.enums.StatusEnum;
import com.zy.ems.utils.exceptions.UserCode;

@Service
@Transactional
public class UserService extends BaseService<UserEntity, Integer> {
    @Autowired
    private UserDao userDao;

    @Override
    public BaseDao<UserEntity, Integer> getEntityDao() {
        return this.userDao;
    }

    /**
     * 根据用户名获取用户
     * 
     * @param userName
     * @return
     */
    public UserEntity getUserEntityByUserName(String userName) {
        return this.userDao.getUserEntityByUserName(userName);
    }

    public UserEntity login(String userName) {
        return this.getUserEntityByUserName(userName);
    }

    public PaginationResult<UserEntity> getUserList(
            Map<String, Object> paramMap) {
        int start = StringUtils.parseInt(paramMap.get("start"));
        int size = StringUtils.parseInt(paramMap.get("size"));
        return this.userDao.findPageList(paramMap, start, size);
    }

    /**
     * 根据用户ID获取已启用用户
     * 
     * @param userId
     * @return
     */
    public UserEntity getEnableUserById(int userId) {
        UserEntity userEntity = this.getById(userId);

        if (userEntity == null) {
            throw new ApiException(UserCode.B_USER_2004);
        }

        if (userEntity.getStatus() == StatusEnum.DISABLE.getCode()) {
            throw new ApiException(UserCode.B_USER_2004);
        }

        return userEntity;
    }

    /**
     * 删除用户
     * 
     * @param userId
     */
    public void deleteUserById(int userId) {
        // 获取账户
        UserEntity userEntity = this.getEnableUserById(userId);

        // 设置状态,软删除
        userEntity.setStatus(StatusEnum.DISABLE.getCode());

        // 更新用户
        this.userDao.update(userEntity);
    }

    /**
     * 编辑用户
     * 
     * @param userEntity
     */
    public void editUser(UserEntity userEntity) {
        if (userEntity == null) {
            return;
        }

        userEntity.setUpdateTime(DateUtil.nowTimeStamp());
        userEntity.setPassword(Md5Util.getPasswordMd5(userEntity.getPassword(),
                userEntity.getSalt()));

        this.userDao.update(userEntity);
    }

    /**
     * 增加用户
     * 
     * @param userEntity
     */
    public void addUser(UserEntity userEntity) {
        if (userEntity == null) {
            return;
        }

        userEntity.setSalt(Md5Util.getPasswordSalt());
        userEntity.setLastLoginIp(Constant.LOCAL_HOST_IP);
        userEntity.setPassword(Md5Util.getPasswordMd5(userEntity.getPassword(),
                userEntity.getSalt()));
        userEntity.setLastLoginTime(0);

        // 默认不启用
        userEntity.setStatus(StatusEnum.DISABLE.getCode());
        userEntity.setUpdateTime(DateUtil.nowTimeStamp());
        userEntity.setCreateTime(DateUtil.nowTimeStamp());

        this.userDao.save(userEntity);
    }

    /**
     * 更改用户状态
     * 
     * @param userId
     * @param status
     */
    public void changeUserStatus(int userId, String status) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userId", userId);
        param.put("status", status);

        this.userDao.updateUserStatus(param);
    }

    /**
     * 更新用户登录日志
     */
    public void updateUserLoginLog(UserEntity userEntity) {
        this.userDao.updateUserLoginLog(userEntity);
    }
}
